package com.example.presentshopping.ui.activity

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.bean.LocationBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.custom.RTextView
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_selectaddress.*


//选择地址
class SelectaddressActivity : BaseMvpActivity() {
    var locationbean: LocationBean? = null
    private var sendMesPresenter: PublicPresenter? = null

    var baseRecyclerAdapter: BaseRecyclerAdapter<LocationBean.DataBean.ListBean>? = null
    override val layoutId: Int
        get() = R.layout.activity_selectaddress

    override fun initView(savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(this, false, "")
        sendMesPresenter?.attachView(this)
        title_main_tv.leftBackImageTv.setOnClickListener { finish() }
        setADadter()
        addResslis()
    }

    fun setADadter() {
        baseRecyclerAdapter = object : BaseRecyclerAdapter<LocationBean.DataBean.ListBean>(this,
            null,
            R.layout.item_selectaddress) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: LocationBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val tvname = holder?.getView<TextView>(R.id.tv_name)
                val tvphone = holder?.getView<TextView>(R.id.tv_phone)
                val tvaddressdetail = holder?.getView<TextView>(R.id.tv_address_detail)
                val text_1 = holder?.getView<RTextView>(R.id.text_1)
                val lv_tsnea = holder?.getView<LinearLayout>(R.id.lv_tsnea)

                val Enterprise: CharArray? = data?.linkman?.toString()?.toCharArray()
                Log.e("setADadter", Enterprise?.get(0).toString());

                tvname?.text = data?.linkman
                tvphone?.text = data?.tel
                tvaddressdetail?.text = data?.address
                text_1?.backgroundColorNormal = Color.parseColor("#cccccc")
                text_1?.text = Enterprise?.get(0).toString()
                lv_tsnea?.setOnClickListener {
                    val intent = Intent()
                    val three: Int =data.id
                    intent.putExtra("three", three) //将计算的值回传回去
                    setResult(2, intent)
                    finish() //结束当前的activity的生命周期

                }
            }

        }
        rcy_viw.adapter = baseRecyclerAdapter
    }

    fun addResslis() {
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getAddressList,
            HttpRequest.getGoodsDetail("")
        )
    }

    override fun showToast(msg: String?) {
        ToastUtil.showMsg(msg)
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override fun onSuccess(data: String?) {
        locationbean = GsonUtils.getGson().fromJson(data, LocationBean().javaClass)
        if (locationbean?.data?.list?.size!! > 0){
            baseRecyclerAdapter?.setNewData(locationbean?.data?.list)
        }else{

        }

    }
}